using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using MimeCloud.AssetManagement.Business.Interface;
using MimeCloud.Foundations.ObjectManagement;

namespace MimeCloud.AssetManagement.DataProvider.Sql.Entities
{
    internal class AssetRepositoryList : IList<IRepository>
    {
        public AssetRepositoryList()
        {
            parentContent = new EntityRef<AssetContent>();
        }

        internal EntitySet<AssetContent_Repository> RepositoryLinks { get { return _repositoryLinks; } set { _repositoryLinks.Assign(value); } }
        public EntitySet<AssetContent_Repository> _repositoryLinks;
        private int id;
        public int ID
        {
            get { return id; }
            set { id = value;_repositoryLinks.ToList().ForEach(sd => sd.AssetContentID = id); }
        }

        public EntityRef<AssetContent> parentContent;
        public ListSelector<AssetContent_Repository, Repository> AssetContentRepositoryList
        {
            get
            {
                if (_AssetContentRepositoryList == null)
                    _AssetContentRepositoryList = this.RepositoryLinks.GetNewBindingList().AsListSelector<AssetContent_Repository, Repository>(
                        link => link.Repository,
                        delegate(IList<AssetContent_Repository> links, Repository r)
                        {
                            links.Add(new AssetContent_Repository
                            {
                                Repository = r,
                                AssetContentID = this.ID,
                                Content=parentContent.Entity,   
                                RepositoryID = r.ID
                            });
                        },
                        delegate(IList<AssetContent_Repository> links, Repository r)
                        {
                            var linkToRemove = links.Where(link => link.RepositoryID == r.ID).Single();
                            Singleton<AssetManagerDataContext>.AppScoped.AssetContentRepositoryLinks.DeleteOnSubmit(linkToRemove);
                            links.Remove(linkToRemove);
                        });
                return _AssetContentRepositoryList;
            }
     
        }
        private ListSelector<AssetContent_Repository, Repository> _AssetContentRepositoryList = null;
        #region IList<IRepository> Members

        public int IndexOf(IRepository item)
        {
            return AssetContentRepositoryList.IndexOf(item as Repository);
        }

        public void Insert(int index, IRepository item)
        {
           AssetContentRepositoryList.Insert(index,item as Repository);
        }

        public void RemoveAt(int index)
        {
            AssetContentRepositoryList.RemoveAt(index);
        }

        public IRepository this[int index]
        {
            get
            {
                return AssetContentRepositoryList[index];
            }
            set
            {
                AssetContentRepositoryList[index] = value as Repository;
            }
        }

        #endregion

        #region ICollection<IRepository> Members

        public void Add(IRepository item)
        {
            if (item as Repository==null)
                throw new NotImplementedException();
            else
            {
                AssetContentRepositoryList.Add(item as Repository);
            }
        }

        public void Clear()
        {
            AssetContentRepositoryList.Clear();
        }

        public bool Contains(IRepository item)
        {
            return AssetContentRepositoryList.Contains(item as Repository);
        }

        public void CopyTo(IRepository[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { return AssetContentRepositoryList.Count; }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(IRepository item)
        {
            return AssetContentRepositoryList.Remove(item as Repository);
        }

        #endregion

        #region IEnumerable<IRepository> Members

        public IEnumerator<IRepository> GetEnumerator()
        {
            return AssetContentRepositoryList.Cast<IRepository>().GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return AssetContentRepositoryList.GetEnumerator();
        }

        #endregion
    }
}
